package com.eastfair.builder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.builder.dto.ReportBuilderPageQuery;
import com.eastfair.builder.dto.ReportBuilderSaveDTO;
import com.eastfair.builder.dto.ReportBuilderUpdateDTO;
import com.eastfair.builder.entity.ReportBuilder;
import com.eastfair.builder.service.ReportBuilderService;
import com.eastfair.builder.vo.ReportBuilderVO;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报馆搭建商信息
 * </p>
 *
 * @author dq
 * @date 2022-08-06
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportBuilder")
@Api(value = "ReportBuilder", tags = "报馆搭建商信息")
@PreAuth(replace = "builder:reportBuilder:")
public class ReportBuilderController extends SuperController<ReportBuilderService, Long, ReportBuilder, ReportBuilderPageQuery, ReportBuilderSaveDTO, ReportBuilderUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportBuilderService reportBuilderService;

    @Override
    public void handlerResult(IPage<ReportBuilder> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportBuilder> reportBuilderList = list
                .stream()
                .map((map) -> ReportBuilder.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportBuilderList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportBuilderPageQuery> params) {
        ReportBuilderPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 报馆ID查询搭建商信息.
     *
     * @param reportSpaceId the report space id
     * @return the by report space id
     */
    @GetMapping("/reportSpace/{reportSpaceId}")
    public R<ReportBuilderVO> getByReportSpaceId(@PathVariable("reportSpaceId") Long reportSpaceId) {
        log.info("getByReportSpaceId - 报馆ID查询搭建商信息, reportSpaceId={}", reportSpaceId);
        return R.successDef(reportBuilderService.getVOByReportSpaceId(reportSpaceId));
    }

    /**
     * 展商ID查询报馆搭建商基本信息.
     *
     * @param exhibitorId the exhibitor id
     * @return the by exhibitor id
     */
    @ApiOperation(value = "展商ID查询报馆搭建商基本信息")
    @GetMapping("/get/exhibitor/{exhibitorId}")
    public R<ReportBuilderVO> getByExhibitorId(@PathVariable("exhibitorId") Long exhibitorId) {
        log.info("getByExhibitorId - 展商ID查询报馆搭建商基本信息, exhibitorId={}", exhibitorId);
        try {
            return R.successDef(reportBuilderService.getVOByExhibitorId(exhibitorId));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
